package com.platform.oss;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.platform.utils.DateUtils;
import com.platform.utils.RRException;
import com.platform.utils.ResourceUtil;

/**
 * 七牛云存储
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-03-25 15:41
 */
public class LocalStorageService extends CloudStorageService {
 
    private String token;

    public LocalStorageService(CloudStorageConfig config) {
        this.config = config;

        //初始化
        init();
    }

    private void init() {
        
    }

    @Override
    public String upload(MultipartFile file) throws Exception {
        String fileName = file.getOriginalFilename();
        String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);
        return upload(file.getBytes(), getPath("local"), getName(prefix));
    }
    public String getPath(String prefix) {
        //生成uuid
        //文件路径
        String path = DateUtils.format(new Date(), "yyyyMMdd") ;

        if (StringUtils.isNotBlank(prefix)) {
            path = prefix + "/" + path;
        }

        return path;
    }
    public String getName(String prefix) {
        //生成uuid
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        //文件路径
        String name =  DateUtils.format(new Date(), "HHmmssS") + uuid.substring(0, 5);



        return name+"." + prefix;
    }
    public String upload(byte[] data, String path,String name) {
    	
        try {
        	String allPath=ResourceUtil.getConfigByName("file.upload")+File.separator+path;
        	createFilePath(allPath, name);
        	writeByte(allPath+File.separator+name, data);
            
        } catch (Exception e) {
            throw new RRException("上传文件失败，请核对七牛配置信息", e);
        }

        return ResourceUtil.getConfigByName("file.appload")+path+"/"+name;
    }
    
	/**
	 * 写字节数组到文件
	 * @param fileName
	 * @param b
	 * @return
	 */
	public static boolean writeByte(String fileName, byte[] b) {
		try {
			BufferedOutputStream fos = new BufferedOutputStream(
					new FileOutputStream(fileName));
			fos.write(b);
			fos.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	private String createFilePath(String tempPath, String fileName) {


		File one = new File(tempPath);
		if (!one.exists()) {
			one.mkdirs();
		}
		return one.getPath() + File.separator + fileName;
	}
    @Override
    public String upload(InputStream inputStream, String path) {
        try {
            byte[] data = IOUtils.toByteArray(inputStream);
            return this.upload(data, path);
        } catch (IOException e) {
            throw new RRException("上传文件失败", e);
        }
    }

	@Override
	public String upload(byte[] data, String path) {
		// TODO Auto-generated method stub
		return null;
	}
}
